﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Rocket.Core;

namespace Rocket.Windows.Framework
{
    public partial class ContainerViewer<H, V, D> : RocketContainer
        , IFormContainerViewer<V>
        where H : IHandler<V>
        where V : BaseObject
        where D: BaseDataViewer
    {
        private D _dataViewerControl;
        private IHandler<V> _handler;
        public ContainerViewer(D dataViewerControl, H handler)
        {
            InitializeComponent();
            _dataViewerControl = dataViewerControl;
            _dataViewerControl.Dock = DockStyle.Fill;
            this.RocketHeight = this.Height + ((Control)_dataViewerControl).Height;
            this.groupBoxFiltering.Controls.Add(_dataViewerControl);

            _handler = handler;
            this.adminController1.RefreshClick += new EventHandler(adminController1_RefreshClick);

            this.SetRocketBindingSource(ref _rocketBindingSource);
            if ((OnRocketBindingSourceSetted != null)
                && (_rocketBindingSource != null))
                OnRocketBindingSourceSetted();
            RocketBindingSource.DataSourceChanged += new EventHandler(RocketBindingSourceChanged);

            if (!string.IsNullOrEmpty(GroupBoxName))
                this.groupBoxFiltering.Text = GroupBoxName;
        }

        void adminController1_RefreshClick(object sender, EventArgs e)
        {
            if ((this.RocketRefresh()) && (OnRocketTaskCompleted != null))
                OnRocketTaskCompleted(sender, e);
        }

        public IHandler<V> IHandler
        {
            get { return _handler; }
        }

        private V _bindedView;
        public virtual V BindedView
        {
            get { return (_bindedView == null) ? (V)Activator.CreateInstance(typeof(V)) : _bindedView; }
        }

        public IRocketViewer<BindingSource> IRocketViewer
        {
            get { return (IRocketViewer<BindingSource>)_dataViewerControl; }
        }

        public int ID
        {
            get { return this._dataViewerControl.ID; }
            set { this._dataViewerControl.ID = value; }
        }

        public event EventHandler OnRocketTaskCompleted;
        public event RocketBindingSourceSetted OnRocketBindingSourceSetted;
        private BindingSource _rocketBindingSource;
        public BindingSource RocketBindingSource
        {
            get { return _rocketBindingSource; }
            set
            {
                _rocketBindingSource = value;
                if ((value != null) && (OnRocketBindingSourceSetted != null))
                    OnRocketBindingSourceSetted();
            }
        }

        public bool RocketLoad()
        {
            this.RocketBindingSource.ResetBindings(false);
            return true;
        }

        public bool RocketRefresh()
        {
            this._dataViewerControl.Refresh();
            return true;
        }

        public void SetRocketBindingSourceView(V dataSource)
        {
            _bindedView = dataSource;
            RocketBindingSource.DataSource = dataSource;
        }

        public void SetRocketBindingSource(ref BindingSource bindingSource)
        {
            this._dataViewerControl.SetRocketBindingSource(ref bindingSource);
        }

        public void RocketBindingSourceChanged(object sender, EventArgs e)
        {
            this._dataViewerControl.RocketBindingSourceChanged((object)this.BindedView, e);
        }

        public string GroupBoxName { get { return _dataViewerControl.GroupBoxName; } set { _dataViewerControl.GroupBoxName = value; } }
    }
}
